package com.hmall.cart.listener;

import com.hmall.cart.service.ICartService;
import com.hmall.common.utils.UserContext;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.stereotype.Component;

import java.util.Set;

import static com.hmall.common.constants.MQConstants.*;

@Component
@Slf4j
@RequiredArgsConstructor
public class OrderCreateListener {


    private final ICartService cartService;

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = CART_CLEAR_QUEUE, durable = "true"),
            exchange = @Exchange(value = TRADE_TOPIC, type = "topic"),
            key = CART_CLEAR_KEY
    ))
    public void listenOrderCreate(Set<Long> itemIds, @Header("userId")Long userId) {
        log.info("订单号已创建，正在删除id为{}的购物车。", itemIds);
        UserContext.setUser(userId);
        cartService.removeByItemIds(itemIds);
    }
}
